package com.js.nickcar.base;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.js.nickcar.R;
import com.js.nickcar.widget.recycler.BaseRecyclerAdapter;
import com.js.nickcar.widget.recycler.RecyclerRefreshLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 李宗源 on 2016/8/3.
 */
public abstract class BaseRecyclerViewFragment<T, P extends BaseRecyclerPresenter> extends BaseFragment<P> implements
        BaseRecyclerView,
        BaseRecyclerAdapter.OnItemClickListener,
        RecyclerRefreshLayout.SuperRefreshLayoutListener,
        BaseRecyclerAdapter.OnItemLongClickListener {

    public RecyclerView mRecyclerView;
    public RecyclerRefreshLayout mRefreshLayout;
    public BaseRecyclerAdapter<T> mAdapter;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_toolbar_recycler;
    }

    @Override
    public void initWidght(View mRoot, LayoutInflater inflater) {
        super.initWidght(mRoot, inflater);
        mRecyclerView = findView(R.id.recyclerView);
        mRefreshLayout = findView(R.id.refreshLayout);
    }

    @Override
    public void initData() {
        mRecyclerView.setLayoutManager(getLayoutManager());//设置布局管理器，默认垂直线性
        mAdapter = getRecyclerAdapter();//获取adapter
        mAdapter.setState(BaseRecyclerAdapter.STATE_HIDE, false);
        mRecyclerView.setAdapter(mAdapter);//设置adapter
        mAdapter.setOnItemClickListener(this);//设置条目点击事件
        mAdapter.setOnItemLongClickListener(this);
        mRefreshLayout.setSuperRefreshLayoutListener(this);//设置下拉刷新和加载更多的监听
        mRefreshLayout.setColorSchemeResources(
                R.color.swiperefresh_color1, R.color.swiperefresh_color2,
                R.color.swiperefresh_color3, R.color.swiperefresh_color4);
        if (isAutoLoad()) {
            mRefreshLayout.setRefreshing(true);//主动刷新，走下拉刷新逻辑
            onRefreshing();
        }
    }

    @Override
    public void onRefreshing() {
        mPresenter.onRefreshing();
    }

    /**
     * 设置适配器
     */
    protected abstract BaseRecyclerAdapter<T> getRecyclerAdapter();

    /**
     * 设置布局管理器
     */
    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getActivity());
    }

    @Override
    protected abstract P createPresenter();

    @Override
    public void refreshSuccess(List data) {
        mAdapter.clear();
        mAdapter.addAll(data);
        mRefreshLayout.setCanLoadMore(true);
    }

    @Override
    public void loadMoreSuccess(ArrayList data) {
        mAdapter.addAll(data);
    }

    @Override
    public void setState(int state, boolean isUpdate) {
        mAdapter.setState(state, isUpdate);
    }

    @Override
    public void onItemClick(int position, long itemId) {
    }

    @Override
    public void onLongClick(int position, long itemId) {
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
        mPresenter.onLoadMore();
    }

    /**
     * 完成
     */
    @Override
    public void onComplete() {
        mRefreshLayout.onComplete();
        mPresenter.setRefreshState(false);
    }

    /**
     * 是否首次自动加载
     *
     * @return
     */
    protected boolean isAutoLoad() {
        return true;
    }

}
